{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a8f4e5c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "#1.1 优化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b9560fba",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY = 'XcZXNUDSqXnWmffDMfHjb1VL'\n",
    "SECRET_KEY = 'Ho76TbgVG20PkOPSpfg5W3SSttGryGAG'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "4bbfbf4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from baidu_speech import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "4fc4b134",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.17fd495ce4825115fd6fa3be6e33b654.2592000.1656581480.282335-26256006'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c_token = fetch_token(API_KEY, SECRET_KEY)\n",
    "c_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "02fcf4af",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"16k.m4a\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9986816a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.153425\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'{\"err_msg\":\"audio trans failed\",\"err_no\":3316,\"sn\":\"42466912751653987081\"}\\n'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr(c_token,\"16k.m4a\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2f33ff29",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.239298\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'result'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-f28334dcea81>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0masr_result\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0meval\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0masr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mc_token\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"16k.m4a\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'result'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0masr_result\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'result'"
     ]
    }
   ],
   "source": [
    "asr_result = eval(asr(c_token,\"16k.m4a\"))['result'][0]\n",
    "asr_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b569c114",
   "metadata": {},
   "outputs": [],
   "source": [
    "#连接图灵机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b8dc4cdf",
   "metadata": {},
   "outputs": [],
   "source": [
    "TL_key = \"d5d5bb66f66d4da8bedb378df7adfa70\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ecf1d2f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests,json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "7253807a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'> {'reqType': 0, 'perception': {'inputText': {'text': '火龙果用英文怎么说'}}, 'userInfo': {'apiKey': 'd5d5bb66f66d4da8bedb378df7adfa70', 'userId': '000001'}}\n",
      "<class 'str'> {\"reqType\": 0, \"perception\": {\"inputText\": {\"text\": \"\\u706b\\u9f99\\u679c\\u7528\\u82f1\\u6587\\u600e\\u4e48\\u8bf4\"}}, \"userInfo\": {\"apiKey\": \"d5d5bb66f66d4da8bedb378df7adfa70\", \"userId\": \"000001\"}}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "TL_url = 'http://openapi.turingapi.com/openapi/api/v2'\n",
    "params = {\n",
    "    \"reqType\":0,\n",
    "    \"perception\": {\n",
    "        \"inputText\": {\n",
    "            \"text\": \"火龙果用英文怎么说\"\n",
    "        },\n",
    "    },\n",
    "    \"userInfo\": {\n",
    "        \"apiKey\": TL_key,\n",
    "        \"userId\": \"000001\"\n",
    "    }\n",
    "}\n",
    "print(type(params),params)\n",
    "print(type(json.dumps(params)),json.dumps(params))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "a4c165e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "res = requests.post(TL_url,data=json.dumps(params))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "b17915b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'pitaya'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res.json()['results'][0]['values']['text']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71c92e80",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d20c9157",
   "metadata": {},
   "outputs": [],
   "source": [
    "#4.语音合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "45f36eb2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'好好好。'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from tuling import TuLing\n",
    "TuLing(TL_key,'你好')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c344afa3",
   "metadata": {},
   "source": [
    "# 4、语音合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "1a7d2a37",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda3\\lib\\site-packages\\pydub\\utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work\n",
      "  warn(\"Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work\", RuntimeWarning)\n"
     ]
    }
   ],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "ef16c3da",
   "metadata": {},
   "outputs": [],
   "source": [
    "from baidu_speech import tts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "1e12f950",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "result saved as :result.wav\n"
     ]
    }
   ],
   "source": [
    "tts(c_token,\"欢迎使用百度语音合成\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "4b56f59a",
   "metadata": {},
   "outputs": [],
   "source": [
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "16f0a8f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting pydub\n",
      "  Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n",
      "Installing collected packages: pydub\n",
      "Successfully installed pydub-0.25.1\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install pydub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "874a3bf6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "499c387b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db01c8e7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4f44be9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5.自动对话机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "0d284dff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.199818\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'result'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-72-ba96599c6a17>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     17\u001b[0m \u001b[1;31m#2.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     18\u001b[0m \u001b[0mc_token\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfetch_token\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mAPI_KEY\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mSECRET_KEY\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 19\u001b[1;33m \u001b[0masr_result\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0meval\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0masr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mc_token\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"16k.m4a\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'result'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     20\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     21\u001b[0m \u001b[1;31m#3\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'result'"
     ]
    }
   ],
   "source": [
    "import speech_recognition\n",
    "from baidu_speech import fetch_token,asr,tts\n",
    "from tuling import TuLing\n",
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "\n",
    "#1.\n",
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"16k.m4a\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "    \n",
    "#2.\n",
    "c_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "asr_result = eval(asr(c_token,\"16k.m4a\"))['result'][0]\n",
    "\n",
    "#3\n",
    "TL_result = TuLing(TL_key,asr_result)\n",
    "print(TL_result)\n",
    "\n",
    "#4\n",
    "tts(c_token,TL_result)\n",
    "\n",
    "\n",
    "#5\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55862ca1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "410de81d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
